class Admin::EnigmasController < Admin::AdminController
  
  def index
    @enigmas = Enigma.paginate(:page => params[:page], :per_page=>25)
  end
  
  def new
    @enigma = Enigma.new
  end
  
  def edit
    @enigma = Enigma.find(params[:id])
  end
  
  def update
    @enigma = Enigma.find(params[:id])
    
    
    if @enigma.update_attributes(params[:enigma])
      flash_notice(:enigma_updated)
      redirect_to admin_enigmas_url
    else
      flash_error(:enigma_not_updated)
      render :edit     
    end
  end
  
  def create
    @enigma = Enigma.new(params[:enigma])
    
    if @enigma.save
      flash_notice(:enigma_created)
      redirect_to admin_enigmas_path
    else 
      flash_error(:enigma_not_created)
      render :new
    end   
  end
  
  def destroy
    @enigma = Enigma.find(params[:id])
    @enigma.destroy
    flash_notice(:enigma_removed)
    redirect_to(admin_enigmas_url) 
  end
  
  def batch_upload
    @batch_content = params[:batch_content]
    #do only new lines
    @batch_content.each("\n") do |phrase|
      text = phrase.lstrip.rstrip 
      puts text
      if !text.empty?
        enigma = Enigma.new
        enigma.enigma = text
        if !enigma.save
          logger.debug("Could not save: "+text)
        end
        
      end
      
    end
    redirect_to admin_enigmas_path
    
  end
  
  def batch_uploader
    @batch_content = ""
  end
  
  
end
